package cn.hd.userService.controller.role;

import cn.hd.commonService.vo.Query;
import cn.hd.commonService.vo.Result;
import cn.hd.commonService.annotations.LogAnnotation;
import cn.hd.commonService.entity.SysRole;
import cn.hd.commonService.enums.MethodType;
import cn.hd.userService.service.role.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/role")
@Api(tags = "角色操作接口")
public class RoleController {

    @Autowired
    private SysRoleService roleService;

    @PostMapping("/save")
    @ApiOperation(value = "保存或修改角色信息")
    @LogAnnotation(methodName = "保存或修改角色信息",methodDesc = "保存或修改角色信息",methodType = MethodType.POST_TYPE)
    public Result saveOrUpdate(SysRole sysRole){
        Assert.notNull(sysRole,"角色信息有误");
        Assert.hasText(sysRole.getRoleName(),"角色名称不能为空");
        roleService.saveOrUpdateRole(sysRole);
        return Result.success("保存成功");
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除角色信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleIds",value = "角色id，以逗号分割",required = true)
    })
    @LogAnnotation(methodName = "删除功能信息",methodDesc = "删除功能信息",methodType = MethodType.DELETE_TYPE)
    public Result deleteBatch(String roleIds){
        Assert.notNull(roleIds,"角色id不能为空");
        roleService.batchDelete(roleIds);
        return Result.success("删除成功");
    }

    @PostMapping("/getListByPage")
    @ApiOperation(value = "分页获取角色的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleName",value = "根据角色名称",required = false)
    })
    @LogAnnotation(methodName = "分页获取角色的信息",methodDesc = "分页获取角色的信息",methodType = MethodType.FIND_TYPE)
    public Result getListByPage(Query query, String roleName){
        return   Result.success("获取成功",roleService.getListByPage(query,roleName));
    }

    @PostMapping("/getFunctionListByRoleId")
    @ApiOperation(value = "通过角色id获取已授权的资源")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId",value = "角色id",required = false)
    })
    @LogAnnotation(methodName = "通过角色id获取已授权的资源",methodDesc = "通过角色id获取已授权的资源",methodType = MethodType.FIND_TYPE)
    public Result getFunctionListByRoleId( Integer roleId){
        Assert.notNull(roleId,"角色id不能为空");
        return   Result.success("获取成功",roleService.getFunctionListByRoleId(roleId));
    }
}
